{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form gg-form-bar border-t border-x">
    <div class="layui-input-inline">
        <input name="buyCode" value="" placeholder="请输入订货号" class="layui-input">
    </div>

    <div class="layui-input-inline">
        <input name="name" value="" placeholder="请输入名称" class="layui-input">
    </div>


    <div class="layui-input-inline">
        <input name="specs" value="" placeholder="请输入规格型号" class="layui-input">
    </div>

    <div class="layui-input-inline">
        <input name="brand" value="" placeholder="请输入品牌" class="layui-input">
    </div>

    <div class="layui-input-inline">
        <input name="htNum" value="" placeholder="请输入合同号" class="layui-input">
    </div>
    <div class="layui-input-inline">
        <input name="pName" value="" placeholder="供应商" class="layui-input">
    </div>
    <div class="layui-input-inline">
        <input name="hsContract" value="" placeholder="货商合同" class="layui-input">
    </div>
    <div class="layui-input-inline">
        <input name="specs" value="" placeholder="型号" class="layui-input">
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <select name="status" >
            <option value="">所有状态</option>
            {foreach ['','草稿','已确认']  as $key=>  $v}
            <option value="{$key}">{$v}</option>
            {/foreach}
        </select>
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="orderDate" placeholder="单据日期" readonly id="orderDate" class="layui-input" autocomplete="off" />
    </div>
    <div class="layui-input-inline">
        <input name="saleUnitName" value="" placeholder="销售单位" class="layui-input">
    </div>
    <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
        <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
    </div>
</form>
<div class="p-3">
    <div>
        <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">+ 添加付款</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger " lay-event="dels" type="button">删除</button>
    </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];

    let statuss=['','草稿','已确认','审批中','已拒绝'];
    let payTypes=['','现金','银行电汇','电子承兑'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool;
        var form =layui.form;
        layui.laydate.render({
            elem:"#orderDate",
            range:'~'
        })
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "index"
                ,toolbar: '#toolbarDemo'
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            // ,defaultToolbar:  ['filter',  'exports']
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                // , {field: 'fkNum', title: '单据编号'}
                 {field: 'pName',  title: '供应商名称'},
                 {field: 'saleUnitName',  title: '销售单位'},
                 {field: 'khh',  title: '开户行'},
                 {field: 'khzh',  title: '开户账户'},
                {field: 'cgHt',  title: '合同单号'},
                {field: 'hsContract',  title: '货商合同'},
                {
                    field: 'brand',
                    title: '品牌',
                    align: 'center',
                },
                {
                    field: 'mdName',
                    title: '品名',
                    align: 'center',
                },
                {
                    field: 'specs',
                    title: '型号',
                    align: 'center',
                },
                {
                    field: 'buyCode',
                    title: '订货号',
                    align: 'center',
                },
                {
                    field: 'unit',
                    title: '计量单位',
                    align: 'center',
                },
                {
                    field: 'bjNum',
                    title: '合同数量',
                    align: 'center',
                },
                {
                    field: 'bjMoney',
                    title: '合同金额',
                    align: 'center',
                },
                , {field: 'money',  title: '付款金额'}
                , {field: 'fkbl',  title: '付款比例'}
                // , {field: 'contractOrder',  title: '关联合同'}
                , {field: 'payType',  title: '付款方式',templet:function (d) {
                        return payTypes[d.payType];
                    }}

                // , {field: 'payBank',  title: '打款银行'}
                , {field: 'payName',  title: '付款人'}
                , {field: 'orderDate',  title: '单据日期'}
                , {field: 'payDate',  title: '实际付款时间'}
                , {field: 'status',  title: '状态',templet:function (d) {
                    return statuss[d.orderStatus];
                    }}
                , {title: '操作', align: 'center'
                    , templet: function (d) {
                        var html = '' +
                            '<button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>' +
                            '';
                        return html;
                    }
                }
            ]]
            , page: true
        });
        //表头工具栏事件
        $('.add-menu').on('click', function () {
            tool.side("add");
            return;
        });
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {
            console.log(obj);
            if (obj.event === 'add') {
                tool.side('add?pid=' + obj.data.id);
                return;
            }
            if (obj.event === 'edit') {
                tool.side('add?id=' + obj.data.orderId);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            obj.del();
                        }
                    }
                    tool.delete("delete", {id: obj.data.id}, callback);
                    layer.close(index);
                });
            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}");
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.orderId)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;
                case 'plqr':

                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        $.ajax({
                            url:"{:url('onSure')}",
                            type:'post',
                            dataType:'json',
                            data:{id:ids.join(',')},
                            success:function (res) {
                                console.log('res',res)
                                layer.msg(res.msg)
                                if (res.code == 0) {
                                }
                            },
                            error(e){
                                layer.msg('网络错误，请稍后重试')
                            }
                        })
                    }else {
                        layer.msg('请选择数据');
                    }
                    break;
            };
        })
    }
</script>
{/block}
<!-- /脚本 -->